﻿Imports Aricie.DNN.Services.Filtering
Imports System.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports System.Xml.Serialization
Imports Aricie.DNN.UI.Attributes

Namespace Aricie.DNN.Services.Url.FriendlyUrl
    <Serializable()> _
    Public MustInherit Class VariableUrlGroup
        Inherits UrlGroup

        <ExtendedCategory("MainSettings")> _
        Public Property Prefix() As String = String.Empty
        <ExtendedCategory("MainSettings")> _
        Public Property Suffix() As String = String.Empty

        <ExtendedCategory("TechnicalSettings")> _
        <NoFlag()> _
        Public Property RewriteType() As RewriteType = RewriteType.Any
        <ExtendedCategory("TechnicalSettings")> _
        Public Property Regex() As String = String.Empty



        Protected Overrides Function ProcessRewrite(ByVal writer As UrlRewriteContext) As GroupRewriteResult
            Dim toReturn As GroupRewriteResult = GetRawVariable(writer)
            Dim prefix As String = Me._Prefix
            Dim suffix As String = Me._Suffix
            Dim atr As AdvancedTokenReplace = Nothing
            If Me.EnableTokenReplace Then
                atr = Me.GetAdvancedTokenReplace(writer.RewriteInput)
                prefix = atr.ReplaceAllTokens(prefix)
                suffix = atr.ReplaceAllTokens(suffix)
            End If
            If toReturn.HasContent Then
                toReturn.RewriteValue = prefix & writer.GetRewriteValue(toReturn, Me) & suffix
                If Not String.IsNullOrEmpty(toReturn.RewriteRegex) Then
                    toReturn.RewriteRegex = prefix & toReturn.RewriteRegex & suffix
                Else
                    If Not String.IsNullOrEmpty(Me.Regex) Then
                        toReturn.RewriteRegex = prefix & FriendlierUrlRule.FormatRegexPattern(Me.Regex) & suffix
                    End If
                End If

            End If

            Return toReturn
        End Function


        Protected Overrides Function ToStringInternal() As String
            Dim innerContent As String
            innerContent = Me.GetName
            'If Me.RewriteType <> FriendlyUrl.RewriteType.Any Then
            '    innerContent &= ", " & Me.RewriteType.ToString()
            'End If
            If Not String.IsNullOrEmpty(Me.Regex) Then
                innerContent = "(" & innerContent & ", " & Me.Regex & ")"
            End If
            Return String.Format("[{0}{1}{2}]", Me.Prefix, innerContent, Me.Suffix)
        End Function




        Protected MustOverride Function GetRawVariable(ByVal writer As UrlRewriteContext) As GroupRewriteResult

        Public Overrides Function GetParamValueChar() As String
            Return MyBase.GetParamValueChar(Me.RewriteType)
        End Function

        Public Overrides Function GetSeparationChar() As String
            Return MyBase.GetSeparationChar(Me.RewriteType)
        End Function


    End Class
End Namespace